<?php

namespace app\admin\controller;
use think\Db;
use think\Controller;

class Chapter extends AdminController
{
    public function select()
    {
        // var_dump(input('id'));die;
        $list = Db::name('chapter')->where('bid ='.input('id'))->order('id', 'desc')->paginate(7);
       // dump($list);exit;
        return view('admin@chapter/index',['list'=>$list]);
    }

    public function add()
    {
        $list = Db::name('txt')->where('end = 1 and id ='.input('id'))->find();
        // var_dump($list);die;
         if (!$list) {
            return $this->error('该书以完结');
         }
        return view('chapter/add',['id'=>input('id')]);
    }

    public function doadd()
    {

       // dump(input('post.'));exit;
//        $p = $request->post();
        // $p = Request::instance()->post();
        // $p = input('post.');
//        var_dump($p);

       // dump(input('post.'));exit;
        $uptime = time();
        $data = [
            'name' => input('post.name'),
            'bid' => input('post.bid'),
            'status' => input('post.status'),
            'uptime' => $uptime,
            'chapter' => input('post.chapter'),
            'content' => input('post.content')

        ];

               // var_dump($data);die;
        $result = Db::name('chapter')->data($data)->insert();

        $res = Db::name('txt')->where('id',input('post.bid'))->update(['end'=>input('post.end')]);

        if ($result > 0) {
            return $this->success('添加成功', url('admin/Chapter/select',['id'=>input('post.bid')]));
        } else {
            return $this->error('添加失败');
        }


    }
    
     public function del($id)
    {
        //        dump($_GET);exit;
           
        $result = Db::name('chapter')->delete($id);
        if ($result > 0) {
            return $this->success('删除成功');
        } else {
            return $this->error('删除失败');
        }
    }


     public function update($id)
    {
        $date = Db::name('Chapter')->find($id);
       // dump($date);exit;
        return view('admin@Chapter/update',['date'=> $date]);

    }

    public function doupdate($id)
    {
       // dump(input('post.'));
        $uptime = time();
        $data = [
            'name' => input('post.name'),
            'status' => input('post.status'),
            'uptime' => $uptime,
            'chapter' => input('post.chapter')

        ];

       // dump($data);exit;
   
        $result = Db::name('chapter')->where('id',$id)->update($data);
       
        if ($result) {
            return $this->success('编辑成功',url('admin/Chapter/select',['id'=>input('post.bid')]));
        } else {
            return $this->error('编辑失败');
        }


    }

}
